<template>
  <div
    class="ball-box"
    :style="{
      '--color': color,
      '--wh': em25,
      '--em5': em5,
      '--em2p5': em2p5,
      '--em_5': em_5,
      '--em2': em2,
      '--em1p5': em1p5,
      '--em1': em1,
      '--em_2': em_2,
    }"
  >
    <div class="ball-container">
      <div class="cube cube--1">
        <div class="side side--back">
          <div class="side__inner"></div>
        </div>
        <div class="side side--left">
          <div class="side__inner"></div>
        </div>
        <div class="side side--right">
          <div class="side__inner"></div>
        </div>
        <div class="side side--top">
          <div class="side__inner"></div>
        </div>
        <div class="side side--bottom">
          <div class="side__inner"></div>
        </div>
        <div class="side side--front">
          <div class="side__inner"></div>
        </div>
      </div>
      <div class="cube cube--2">
        <div class="side side--back">
          <div class="side__inner"></div>
        </div>
        <div class="side side--left">
          <div class="side__inner"></div>
        </div>
        <div class="side side--right">
          <div class="side__inner"></div>
        </div>
        <div class="side side--top">
          <div class="side__inner"></div>
        </div>
        <div class="side side--bottom">
          <div class="side__inner"></div>
        </div>
        <div class="side side--front">
          <div class="side__inner"></div>
        </div>
      </div>
      <div class="cube cube--3">
        <div class="side side--back">
          <div class="side__inner"></div>
        </div>
        <div class="side side--left">
          <div class="side__inner"></div>
        </div>
        <div class="side side--right">
          <div class="side__inner"></div>
        </div>
        <div class="side side--top">
          <div class="side__inner"></div>
        </div>
        <div class="side side--bottom">
          <div class="side__inner"></div>
        </div>
        <div class="side side--front">
          <div class="side__inner"></div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, watch } from 'vue';

// 定义 props
const props = defineProps({
  options: {
    type: Object,
    default: () => ({})
  }
});

// 响应式状态
const em5 = ref('50px');
const em2p5 = ref('25px');
const em_5 = ref('-50px');
const em2 = ref('20px');
const em_2 = ref('-20px');
const em25 = ref('250px');
const em1 = ref('10px');
const em1p5 = ref('15px');
const color = ref('rgb(113, 192, 255)');

// 监听 options.color 变化
watch(() => props.options?.color, (val) => {
  if (val) {
    color.value = val;
  }
}, { immediate: true });

// 监听 options.width 变化
watch(() => props.options?.width, (val) => {
  if (val) {
    em5.value = `${(val * 50) / 250}px`;
    em2p5.value = `${(val * 25) / 250}px`;
    em_5.value = `${(val * -50) / 250}px`;
    em2.value = `${(val * 20) / 250}px`;
    em_2.value = `${(val * -20) / 250}px`;
    em25.value = `${(val * 250) / 250}px`;
    em1p5.value = `${(val * 15) / 250}px`;
    em1.value = `${(val * 10) / 250}px`;
  }
}, { immediate: true });
</script>

<style scoped lang="less">
.ball-box {
  width: 100%;
  height: 100%;
  position: relative;
  display: flex;
  align-items: center;
}

.ball-container {
  width: var(--wh);
  height: var(--wh);
  -webkit-transform-style: preserve-3d;
  transform-style: preserve-3d;
  -webkit-animation: rotate 32s infinite linear;
  animation: rotate 42s infinite linear;
  margin: 0 auto;
}

.cube {
  position: absolute;
  width: var(--wh);
  height: var(--wh);
  -webkit-transform-style: preserve-3d;
  transform-style: preserve-3d;
}
.cube--2 {
  -webkit-transform: rotateX(45deg) rotateY(45deg);
  transform: rotateX(45deg) rotateY(45deg);
}
.cube--3 {
  -webkit-transform: rotateX(45deg) rotateZ(45deg);
  transform: rotateX(45deg) rotateZ(45deg);
}

.side {
  position: absolute;
  width: var(--wh);
  height: var(--wh);
  border: 1px dotted var(--color);
  border-radius: 50%;
  -webkit-transform-style: preserve-3d;
  transform-style: preserve-3d;
  -webkit-transform: rotateY(180deg);
  transform: rotateY(180deg);
}
.side::before,
.side::after {
  content: '';
  display: block;
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  top: 0;
  margin: auto;
  box-sizing: border-box;
  border-radius: 15px;
  border: 1px solid;
  box-shadow: inset 0 0 var(--em2), 0 0 var(--em2);
}
.side::before {
  width: var(--em2p5);
  height: var(--em2p5);
  color: var(--color);
}
.side::after {
  width: var(--em1p5);
  height: var(--em1p5);
  -webkit-transform: translateZ(var(--em_2));
  transform: translateZ(var(--em_2));
  box-shadow: inset 0 0 var(--em1), 0 0 var(--em1);
  color: var(--color);
}

.side--back {
  -webkit-transform: translateZ(var(--em_5)) rotateY(180deg);
  transform: translateZ(var(--em_5)) rotateY(180deg);
}

.side--left {
  -webkit-transform: translateX(var(--em_5)) rotateY(-90deg);
  transform: translateX(var(--em_5)) rotateY(-90deg);
}

.side--right {
  -webkit-transform: translateX(var(--em5)) rotateY(90deg);
  transform: translateX(var(--em5)) rotateY(90deg);
}

.side--top {
  -webkit-transform: translateY(var(--em_5)) rotateX(90deg);
  transform: translateY(var(--em_5)) rotateX(90deg);
}

.side--bottom {
  -webkit-transform: translateY(var(--em5)) rotateX(-90deg);
  transform: translateY(var(--em5)) rotateX(-90deg);
}

.side--front {
  -webkit-transform: translateZ(var(--em5));
  transform: translateZ(var(--em5));
}

.side__inner {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  top: 0;
  margin: auto;
  border-radius: inherit;
  border: 1px solid;
  color: var(--color);
  -webkit-transform-style: preserve-3d;
  transform-style: preserve-3d;
}
.side__inner::before,
.side__inner::after {
  content: '';
  display: block;
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  top: 0;
  margin: auto;
  box-sizing: border-box;
  border-radius: 50%;
  border: 1px solid;
}
.side__inner::before {
  color: var(--color);
}
.side__inner::after {
  color: var(--color);
}

@-webkit-keyframes rotate {
  100% {
    -webkit-transform: rotateX(360deg) rotateY(720deg) rotateZ(360deg);
    transform: rotateX(360deg) rotateY(720deg) rotateZ(360deg);
  }
}

@keyframes rotate {
  100% {
    -webkit-transform: rotateX(360deg) rotateY(720deg) rotateZ(360deg);
    transform: rotateX(360deg) rotateY(720deg) rotateZ(360deg);
  }
}
</style>
